package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.AlipayInfo;
import cc.rengu.redp.bizimpl.upmp.service.AlipayInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * 支付宝类目的控制类
 *
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@RestController
@Api(tags = "参数管理:支付宝类目管理")
@RequestMapping("/admin/merParam/alipayInfo")
public class AlipayInfoController extends BaseController {

    @Autowired
    private AlipayInfoService alipayInfoService;

    @GetMapping
//    @RequiresPermissions("alipayInfo:view")
    public Map<String, Object> list(QueryRequest request, AlipayInfo alipayInfo) {
        return getDataTable(this.alipayInfoService.findByPage(request, alipayInfo));
    }

    @Log("查询指定id类目")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "类目id", required = true, dataType = "String")
    public AlipayInfo getOne(@NotBlank(message = "{required}") String id) {
        return alipayInfoService.getById(id);
    }

    @Log("新增类目")
    @PostMapping
//    @RequiresPermissions("alipayInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "alipayNo", value = "支付宝类目代码",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "apFstName", value = "支付宝类目描述",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "apSecName", value = "支付宝二级类目描述",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "apThiName", value = "支付宝三级类目描述",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "apThiCode", value = "支付宝三级类目编码", required = true, dataType = "String")
    })
    public OperationResult add(@Valid AlipayInfo alipayInfo) throws RedpException {
        try {
            AlipayInfo info = this.alipayInfoService.getByUk(alipayInfo.getAlipayNo());
            if (null != info) {
                return OperationResult.buildFailureResult("[ 支付宝类目代码 ] 重复，请重新输入");
            }
            this.alipayInfoService.createAlipayInfo(alipayInfo);
        } catch (Exception e) {
            String message = "新增类目失败";
            log.error(message, e);
            throw new RedpException(message);
        }
        return OperationResult.buildSuccessResult();
    }

    @Log("修改类目")
    @PutMapping("/update")
//    @RequiresPermissions("alipayInfo:update")
    public OperationResult update(@Valid AlipayInfo alipayInfo) throws RedpException {
        try {
            this.alipayInfoService.updateAlipayInfo(alipayInfo);
        } catch (Exception e) {
            String message = "修改类目失败";
            log.error(message, e);
            throw new RedpException(message);
        }
        return OperationResult.buildSuccessResult();
    }

    @Log("注销类目")
    @PutMapping("/cancel")
//    @RequiresPermissions("alipayInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "类目id", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.alipayInfoService.cancelAlipayInfo(id);
        } catch (Exception e) {
            String message = "注销类目失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除类目")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("alipayInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "类目id，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.alipayInfoService.deleteAlipayInfo(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除类目失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "alipayNo", value = "类目代码", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "apFstName", value = "类目描述", dataType = "String")
    })
    public RedpResponse check(String id, String alipayNo, String apFstName) throws RedpException {
        LambdaQueryWrapper<AlipayInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(alipayNo)) {
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(AlipayInfo::getId, id);
            }
            wrapper.eq(AlipayInfo::getAlipayNo, alipayNo);
            AlipayInfo info = alipayInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 支付宝类目代码 ] 重复，请重新输入");
            }
        }
        if (StringUtils.checkValNotNull(apFstName)) {
            wrapper = new LambdaQueryWrapper<AlipayInfo>();
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(AlipayInfo::getId, id);
            }
            wrapper.eq(AlipayInfo::getApFstName, apFstName);
            AlipayInfo info = alipayInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 支付宝类目描述 ] 重复，请重新输入");
            }
        }
        return new RedpResponse().message("验证成功");
    }
}
